package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.guli.common.base.result.R;
import com.atguigu.guli.service.edu.entity.Chapter;
import com.atguigu.guli.service.edu.entity.Video;
import com.atguigu.guli.service.edu.feign.VodFileService;
import com.atguigu.guli.service.edu.service.ChapterService;
import com.atguigu.guli.service.edu.service.VideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author liujun
 * @since 2021-01-20
 */
//@CrossOrigin
@Api(description = "章节管理")
@Slf4j
@RestController
@RequestMapping("/admin/edu/chapter")
public class ChapterController {
    @Autowired
    private ChapterService chapterService;
    @Autowired
    private VideoService videoService;
    @Autowired
    private VodFileService vodFileService;
    @ApiOperation("新增章节")
    @PostMapping("/save")
    public R save(
    @ApiParam(value="章节对象", required = true)
        @RequestBody Chapter chapter){
        boolean result = chapterService.save(chapter);
        if (result) {
            return R.ok().message("保存成功");
        } else {
            return R.error().message("保存失败");
        }
    }

    @ApiOperation("删除章节")
    @DeleteMapping("/remove/{id}")
    @Transactional(rollbackFor = Exception.class)
    public R removeById(
            @ApiParam(value="章节id", required = true)
            @PathVariable(value = "id") String id){
        //todo 删除章节中的视频
        //1、先删除阿里云上的视频
        final List<Video> videos = videoService.list(new QueryWrapper<Video>().eq("chapter_id", id));
        if(!CollectionUtils.isEmpty(videos)){
            final List<String> videoIds = videos.stream().map(Video::getVideoSourceId).collect(Collectors.toList());
            if(!CollectionUtils.isEmpty(videoIds)){
                vodFileService.removeVideoByIdList(videoIds);
            }
        }
        //2、再删除数据库中的视频记录
        videoService.remove(new QueryWrapper<Video>().eq("chapter_id", id));
        //删除章节
        final boolean res = chapterService.removeById(id);
        if (res) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("删除失败");
        }
    }
    @ApiOperation("根据id查找章节")
    @GetMapping("/get/{id}")
    public R getById(
            @ApiParam(value="章节id", required = true)
            @PathVariable(value = "id") String id){
        final Chapter chapter = chapterService.getById(id);
        if (chapter !=null) {
            return R.ok().data("item",chapter).message("查找成功");
        } else {
            return R.error().message("查找失败");
        }
    }

    @ApiOperation("更新章节")
    @PutMapping("/update")
    public R updateById(
            @ApiParam(value="章节", required = true)
            @RequestBody Chapter chapter){
        final boolean res = chapterService.updateById(chapter);
        if (res) {
            return R.ok().message("更新成功");
        } else {
            return R.error().message("更新失败");
        }
    }

    @ApiOperation("根据courseId查找内嵌树形集合")
    @GetMapping("/nested-list/{courseId}")
    public R getNestedTreeList(
            @ApiParam(value="课程id", required = true)
            @PathVariable(value = "courseId") String courseId){
        //通过courseId找到章节集合
        final List<Chapter> chapters = chapterService.list(new QueryWrapper<Chapter>().eq("course_id", courseId).orderByAsc("sort"));
        chapters.forEach(chapter -> {
            //通过chapter_id和course_id找到视频集合
            final List<Video> videos = videoService.list(new QueryWrapper<Video>().eq("course_id", courseId).eq("chapter_id", chapter.getId()).orderByAsc("sort"));
            chapter.setChildren(videos);
        });
        if (chapters !=null) {
            return R.ok().data("items",chapters).message("查找成功");
        } else {
            return R.error().message("查找失败");
        }
    }
}

